package com.returnsoft.softphone.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.softphone.dto.SessionTypeDto;
import com.returnsoft.softphone.eao.CampaignEao;
import com.returnsoft.softphone.eao.SessionTypeEao;
import com.returnsoft.softphone.entity.Campaign;
import com.returnsoft.softphone.entity.SessionType;
import com.returnsoft.softphone.exception.ConversionExcepcion;
import com.returnsoft.softphone.exception.CCenterExcepcion;
import com.returnsoft.softphone.exception.EaoExcepcion;
import com.returnsoft.softphone.util.Conversion;

/**
 * Session Bean implementation class SessionTypeBean
 */
@Stateless
@LocalBean
public class SessionTypeBean implements SessionTypeInterface, Serializable {
	private static final long serialVersionUID = 1L;
	@EJB
	private SessionTypeEao typeEao;
	@EJB
	private CampaignEao campaignEao;
	@EJB
	private Conversion conv;

	@Override
	public List<SessionTypeDto> findTypesByAgent(Integer agentId)
			throws CCenterExcepcion {

		try {

			List<Campaign> campaigns = campaignEao.findByAgent(agentId);
			List<SessionTypeDto> sessionTypesDto = new ArrayList<SessionTypeDto>();
			for (Campaign campaign : campaigns) {
				List<SessionType> sessionTypes = typeEao.findType(campaign
						.getId());
				for (SessionType sessionType : sessionTypes) {
					SessionTypeDto sessionTypeDto = conv
							.fromSessionType(sessionType);
					Boolean exist = Boolean.FALSE;
					for (int i = 0; i < sessionTypesDto.size(); i++) {
						SessionTypeDto sessionTypeDtoV = sessionTypesDto.get(i);
						if (sessionTypeDtoV.getId().equals(
								sessionTypeDto.getId())) {
							exist = Boolean.TRUE;
						}
					}
					if (!exist) {
						sessionTypesDto.add(sessionTypeDto);
					}
				}
			}
			return sessionTypesDto;

		} catch (EaoExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new CCenterExcepcion(e.getMessage());
		}

	}
	
	@Override
	public List<SessionTypeDto> findTypesBySupervisor(Integer supervisorId)
			throws CCenterExcepcion {

		try {

			List<Campaign> campaigns = campaignEao.findBySupervisor(supervisorId);
			List<SessionTypeDto> sessionTypesDto = new ArrayList<SessionTypeDto>();
			for (Campaign campaign : campaigns) {
				List<SessionType> sessionTypes = typeEao.findType(campaign
						.getId());
				for (SessionType sessionType : sessionTypes) {
					SessionTypeDto sessionTypeDto = conv
							.fromSessionType(sessionType);
					Boolean exist = Boolean.FALSE;
					for (int i = 0; i < sessionTypesDto.size(); i++) {
						SessionTypeDto sessionTypeDtoV = sessionTypesDto.get(i);
						if (sessionTypeDtoV.getId().equals(
								sessionTypeDto.getId())) {
							exist = Boolean.TRUE;
						}
					}
					if (!exist) {
						sessionTypesDto.add(sessionTypeDto);
					}
				}
			}
			return sessionTypesDto;

		} catch (EaoExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new CCenterExcepcion(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new CCenterExcepcion(e.getMessage());
		}

	}
	

}
